<?php

class TXArray {

    public static function searchRecursive($needle, $haystack, $strict = false) {
        if (!($haystack instanceof RecursiveArrayIterator)) {
            $ai = new RecursiveArrayIterator($haystack);
        }

        while ($ai->valid()) {
            if (is_scalar($ai->current())) {
                if (($strict && $needle === $ai->current()) || (!$strict && $needle == $ai->current()))
                    return true;
            } else {
                if (($strict && $needle === $ai->current()) || (!$strict && $needle == $ai->current())) {
                    return true;
                } else {
                    if (self::searchRecursive($needle, $ai->current(), $strict))
                        return true;
                }
            }

            $ai->next();
        }

        return false;
    }

    public static function diff($oldArray, $newArray) {
        return array(
            'add' => array_diff($newArray, $oldArray),
            'del' => array_diff($oldArray, $newArray),
        );
    }
	
	public static function mergeRecursive($arr) {
		$args = func_get_args();
		if (count($args) == 1)
			return array_shift($args);
		else if (count($args) > 2) {
			$r = array();
			while (count($args))
				$r = self::mergeRecursive($r, array_shift($args));
			return $r;
		}
		
		$arr1 = array_shift($args);
		$arr2 = array_shift($args);
		
		foreach ($arr2 as $k => $v) {
			if (isset($arr1[$k])) {
				if (is_array($arr1[$k])) {
					$arr1[$k] = self::mergeRecursive($arr1[$k], $v);
				} else {
					$arr1[$k] = $v;
				}
			} else {
				$arr1[$k] = $v;
			}
		}
		return $arr1;
	}

}
